// pages/seat/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    course_id:'',
    courseData: {},
    seats: {},
    students: [],
    curr_seat: '',
    index: 0,
    student_id:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    this.setData({
      course_id: options.course_id
    })
  },
  onShow: function (options) {    
    wx.showLoading({
      title: '加载中',
    }) 
    //获取课程
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/course/info',
      data: {
        id: this.data.course_id
      },
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {        
        this.setData({
          courseData: res.data.data,
          seats: res.data.data['seats']
        })
      }      
    })    
    
    //获取学员
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/getuserstudents',
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {        
        if (res.data.data[0]) {
          this.setData({
            students: res.data.data,
            student_id: res.data.data[0]['id']
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '您还没有相关学员，立即添加？',
            success: res => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/addstudent/index',
                })
              }                 
            }
          })
        }
      }
    })

    
    wx.hideLoading()
    
  },
  onChoose: function(res){ 
    if (!this.data.student_id) {
      wx.showModal({
        title: '提示',
        content: '您还没有相关学员，立即添加？',
        success: res => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/addstudent/index',
            })
          }
        }
      })
      return;
    }  
    if (!app.globalData.userInfo.mobile) {
      wx.showModal({
        title: '提示',
        content: '您还没绑定手机号码，立即绑定？',
        success: res => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/myphone/index',
            })
          }                    
        }        
      })
      return;
    }
    if (this.data.student_id) {
      this.setData({
        curr_seat: res.currentTarget.dataset.seat
      })
    }
  },  
  bindPickerChange: function (e) {     
    this.setData({
      student_id: this.data.students[e.detail.value]['id'],
      index: e.detail.value
    })
  },
  toPayMent: function(e){
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/addorder',
      data: {
        course_id: this.data.courseData.id,
        seat: this.data.curr_seat,        
        student_id: this.data.student_id
      },
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {
        if (res.data.code){
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp.toString(),
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': 'MD5',
            'paySign': res.data.data.paySign,
            success(res) {
              wx.switchTab({
                url: '/pages/mystudy/index',
              })
            },
            fail(res) {
              wx.switchTab({
                url: '/pages/mystudy/index',
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
            duration: 2000
          })
        }       
      }
    })
  }
})